package com.ews.onlineexchange.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.xml.bind.annotation.XmlTransient;

import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.Analyzer;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Store;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;


@Entity
@Table(name = "SYSTEM_USER")
@Inheritance(strategy = InheritanceType.JOINED)
@Analyzer(impl = SmartChineseAnalyzer.class)
public class User extends DataEntity {
	/**
	 * 
	 */
	private static final long		serialVersionUID		= 1L;

	private String					username;
	private String					password = "WnpyakA4MzA5ODk5";
	private String 					shiroPwd;
	private String					email;
	private String					cnname;
	private List<Category>			auditableCategories		= new ArrayList<Category>();
	private List<Category>			regulatedCategories		= new ArrayList<Category>();
	protected RejectRecord			currRejectRecord;
	protected List<RejectRecord>	historyRejectRecords	= new ArrayList<>();

	protected Boolean				auditted;
	protected String				seqNo;
	protected String region; 		//所在地区
	protected String 				roles;
	protected String				sdzwJson; //山东政务返回的用户信息json

	public String getShiroPwd() {
		return shiroPwd;
	}

	public void setShiroPwd(String shiroPwd) {
		this.shiroPwd = shiroPwd;
	}

	public String getRoles() {
		return roles;
	}

	public void setRoles(String roles) {
		this.roles = roles;
	}

	public String getSdzwJson() {
		return sdzwJson;
	}

	public void setSdzwJson(String sdzwJson) {
		this.sdzwJson = sdzwJson;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}
	
	@NotNull
	@Column(unique = true)
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = StringUtils.trim(username);
	}

//	@Pattern(regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&_])[A-Za-z\\d$@$!%*?&_]{8,}", message = "密码必须以字母开头，长度至少8位，需包含大小写字母、数字和特殊字符")
	@Transient
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@NotNull
	@Email
	@Column(unique = true)
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = StringUtils.trim(email);
	}

	@NotNull
	@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
	public String getCnname() {
		return cnname;
	}

	public void setCnname(String cnname) {
		this.cnname = cnname;
	}

	@OneToOne(cascade = CascadeType.ALL)
	public RejectRecord getCurrRejectRecord() {
		return currRejectRecord;
	}

	public void setCurrRejectRecord(RejectRecord currRejectRecord) {
		this.currRejectRecord = currRejectRecord;
	}

	@Field(analyze = Analyze.NO)
	public Boolean getAuditted() {
		return auditted;
	}

	public void setAuditted(Boolean auditted) {
		this.auditted = auditted;
	}

	@Column(unique = true)
	public String getSeqNo() {
		return seqNo;
	}

	public void setSeqNo(String seqNo) {
		this.seqNo = seqNo;
	}
	
	@XmlTransient
	@ManyToMany
	public List<Category> getAuditableCategories() {
		return auditableCategories;
	}

	public void setAuditableCategories(List<Category> auditableCategories) {
		this.auditableCategories = auditableCategories;
	}
	@XmlTransient
	@ManyToMany
	public List<Category> getRegulatedCategories() {
		return regulatedCategories;
	}

	public void setRegulatedCategories(List<Category> regulatedCategories) {
		this.regulatedCategories = regulatedCategories;
	}
	
	@XmlTransient
	@OneToMany(cascade = CascadeType.ALL)
	@OrderBy("createDate DESC")
	public List<RejectRecord> getHistoryRejectRecords() {
		return historyRejectRecords;
	}

	public void setHistoryRejectRecords(List<RejectRecord> historyRejectRecords) {
		this.historyRejectRecords = historyRejectRecords;
	}

	@Transient
	public boolean isCityUser(){
		if(StringUtils.isNotEmpty(region) && "370501".equals(region))return true;
		else return false;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((username == null) ? 0 : username.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}
}
